//
//  GMHomeHeadView.m
//  Teecloud
//
//  Created by 小熊 on 2018/10/16.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "GMHomeHeadView.h"
#import "FMMyCountsModel.h"
#import "GMFirstCollectionViewCell.h"
#import "GMHeaderModel.h"
@interface GMHomeHeadView ()<SDCycleScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UIView *banaerView;
@property (weak, nonatomic) IBOutlet UIView *bagView;
@property (nonatomic, strong) SDCycleScrollView *cyScrollview;
@property (nonatomic, strong) UICollectionView *colView;
@property (nonatomic, strong) NSMutableArray *topArr;
@property (nonatomic, strong) NSMutableArray *groupArr;
@end
@implementation GMHomeHeadView
-(void)awakeFromNib{
    [super awakeFromNib];
    [self.banaerView addSubview: self.cyScrollview];
    [self.bagView addSubview:self.colView];
   
}
-(NSMutableArray *)groupArr{
    if (!_groupArr) {
        _groupArr = [[NSMutableArray alloc] init];
    }
    return _groupArr;
}
-(void)setBaseModel:(GMHomeBaseModel *)baseModel{
    _baseModel = baseModel;
    [self.groupArr removeAllObjects];
    [self.topArr removeAllObjects];
    [self.topArr addObjectsFromArray:baseModel.Tops];
    NSMutableArray *titArr =[[NSMutableArray alloc] init];
    NSMutableArray *imgArr =[[NSMutableArray alloc] init];
    for (NSInteger i =0; i<self.topArr.count; i++) {
        GMTopModel *topModel =self.topArr[i];
        [titArr addObject:topModel.Title];
        [imgArr addObject:topModel.HeadUrl];
    }
    self.cyScrollview.imageURLStringsGroup =imgArr;
    self.cyScrollview.titlesGroup = titArr;
    NSArray *titarray =@[@"我的课件",@"我的消息",@"我的问题",@"我的错题本",@"我的考试",@"我的班组社团"];
    NSArray *imgarray =@[@"my-courseware",@"my-message",@"my-question",@"my-error",@"my-test",@"my-group"];
    for (NSInteger i =0; i<titarray.count; i++) {
        GMHeaderModel *model =[[GMHeaderModel alloc] init];
        model.titName = titarray[i];
        model.imgUrl = imgarray[i];
        switch (i) {
            case 0:
                model.count =[NSString stringWithFormat:@"(%@)",baseModel.MyCounts.MyResCount];
                break;
            case 1:
                model.count =[NSString stringWithFormat:@"(%@/%@)",[XGMArchiverTools cacheForKey:@"myNewMsgCount"],baseModel.MyCounts.MyMsgCount];
                model.MyNewCount = [NSString stringWithFormat:@"%@",[XGMArchiverTools cacheForKey:@"myNewMsgCount"]];
                break;
            case 2:
                model.count =[NSString stringWithFormat:@"(%@/%@)",baseModel.MyCounts.MyNewQuestCount,baseModel.MyCounts.MyQuestCount];
                 model.MyNewCount = baseModel.MyCounts.MyNewQuestCount;
                break;
            case 3:
                model.count =[NSString stringWithFormat:@"(%@)",baseModel.MyCounts.MyErrCount];
                break;
            case 4:
               model.count =[NSString stringWithFormat:@"(%@/%@)",baseModel.MyCounts.MyNewTestCount,baseModel.MyCounts.MyTestCount];
                 model.MyNewCount = baseModel.MyCounts.MyNewTestCount;
                break;
            case 5:
                model.count =[NSString stringWithFormat:@"(%@)",baseModel.MyCounts.MyGroupCount];
                break;
                
            default:
                break;
        }
        [self.groupArr addObject:model];
    }
    [self.colView reloadData];
}
- (NSMutableArray *)topArr{
    if (!_topArr) {
        _topArr = [[NSMutableArray alloc] init];
    }
    return _topArr;
}
- (SDCycleScrollView *)cyScrollview{
    if (!_cyScrollview) {
        _cyScrollview = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, 180) delegate:self placeholderImage:[UIImage imageNamed:@"dsss"]];
        _cyScrollview.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
        _cyScrollview.delegate = self;
        _cyScrollview.autoScrollTimeInterval = 5;
    }
    return _cyScrollview;
}
-(UICollectionView *)colView{
    if (!_colView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _colView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, 130) collectionViewLayout:layout];
        _colView.dataSource =self;
        _colView.delegate = self;
        _colView.scrollEnabled = NO;
        _colView.backgroundColor = kRGB(245, 245, 245);
        [_colView registerNib:[UINib nibWithNibName:@"GMFirstCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"GMFirstCollectionViewCell"];
    }
    return _colView;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.groupArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GMFirstCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"GMFirstCollectionViewCell" forIndexPath:indexPath];
    GMHeaderModel *model = self.groupArr[indexPath.row];
    cell.groModel = model;
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(DEVICE_WIDTH/3-1, 60);
    
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(5, 0, 5, 0);
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 1;
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
     GMHeaderModel *model = self.groupArr[indexPath.row];
     self.clickBlock(model.titName);
}
/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    GMTopModel *model =self.topArr[index];
    if (self.homeBannerBlock) {
        self.homeBannerBlock(model);
    }
    
}
@end
